package com.hzdl.house_mgr.controller;

import com.hzdl.house_mgr.bean.FaceSpot;
import com.hzdl.house_mgr.bean.House;
import com.hzdl.house_mgr.bean.User;
import com.hzdl.house_mgr.service.HouseService;
import com.hzdl.house_mgr.service.UserService;
import com.hzdl.house_mgr.util.AjaxResult;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("faceLogin")
public class FaceLoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private HouseService houseService;

    @RequestMapping("faceLoginInit")
    public String faceLoginInit(){
        return "client/faceLoginTest";
    }

    @RequestMapping("searchFace")
    @ResponseBody
    public String searchFace(String img, HttpSession session) {
        String faceId = "1";
        JSONObject js = FaceSpot.searchFace(img, "1", faceId);
        /*System.out.println(js.toString(1));*/
       /* js.result.user_list[0].score*/
        /*String result = js.getString("user_id");
        System.out.println(js.getString("user_id"));*/
        JSONObject result = js.getJSONObject("result");
        JSONArray user_list = result.getJSONArray("user_list");
        JSONObject o = (JSONObject)user_list.get(0);
        String user_id = o.getString("user_id");
        /*还需要根据userId找对应的用户所有数据，然后setSession，数据库需新增一个人脸识别的faceId*/
        User user = userService.findUserByFace(faceId);
        if(user != null){
            session.setAttribute("session_user",user);
        }

        List<House> all = houseService.findAll();
        session.setAttribute("count",houseService.getCount());
        session.setAttribute("houses",all);
        return js.toString();
    }
}
